<?php
/**
 * Modify by 			peace@warmyc.n
 * Creat Date			2009-03-11
 */
cache_delete_manage();
$cmd = isset($_GET['cmd'])? strtolower($_GET['cmd']) : 'list';
switch($cmd)
{
	case "add":
		Add();
		break;
	case "edit":
		Edit();
		break;
	case "delete":
		Del();
		break;
	case "save":
		Save();
		break;
	default:
		$cmd	= "list";
		View();
		break;
}
/**************************************************************************************************
功能列表
**************************************************************************************************/
function View()
{
	set_cookie("SYSTEM_HTTP_REFERER", $_SERVER["REQUEST_URI"]);
	/**********************************************************************************************
	获取所有功能点
	**********************************************************************************************/
	$where = "";
	$sql = "select * from `".GROUPNAMETABLE."` $where order by Id";
	
	$row = $GLOBALS['db']->query_row($sql,MYSQL_ASSOC);
	
?>
    <table Class="yc" cellspacing="1" cellpadding="0">    
	<tr>
	    <th colspan="2" width="100%">角色名</th>
      </tr>
		<?
			for($i=0;$i<count($row);$i++)
		{
			 print("<tr>");
			 print("<td width=\"100%\">");
			 print("<a href=\"".ATHENA_SCRIPT_INDEX."?Model=".$GLOBALS['ATHENA_MODEL']."&cmd=edit&Id=".$row[$i]['id']."\">");
			 print($row[$i]['name']);
			 print("</a></td>");
			 print("</tr>");
		}
		?>	
    </table>
<br>


<?}
function Edit()
{
	//获取设置权限的用户组
	$group			= isset($_GET['Id']) && ereg("^[0-9]+$",$_GET['Id']) ? $_GET['Id'] : 0;
	if($group == 0)
		ANotify::halt("请选择需要设置权限的组！",'ERR');
	//初始化当前组权限
	$group_right	= new warmy_right($GLOBALS['db'],$group,'G');//print_r($group_right->right);exit;
	//判断当前用户是否隶属超级管理员组
	//获取所有功能点
	if(array_search(1,$GLOBALS['USER_RIGHT']['GROUPS'])!==false)
	{
		$tree_all 		= new Warmy_tree($GLOBALS['db'],"");
	}
	else 
		$tree_all 		= new Warmy_tree($GLOBALS['db'],$GLOBALS['USER_RIGHT']['RIGHTS']);
	$sql			= "select Name from `".GROUPNAMETABLE."` where Id='$group'";
	$name			= $GLOBALS['db']->query_value($sql);
	
?>
	  <table Class="yc" cellspacing="1" cellpadding="0">  
	  <tr>
	    <td id="right_list">
		  <ul><?$tree_all->PrintTreeEx($group_right->right);?></ul>
		</td>
	  </tr>
	  <tr bgcolor="#FFFFFF">
		<td colspan="10" height="50" style="text-align:center">
			<input name="Submit" type="submit" class="button" value="保存" onclick="AppendCmd('save')"/>
	      	<input name="back" type="submit" class="button" value="返回" onclick="AppendCmd('back')"/>
	      	<input type="hidden" name="SYSTEM_HTTP_REFERER" value="<?=get_cookie("SYSTEM_HTTP_REFERER")?>">
			<input type="hidden" name="ID" value="<?=$group?>">
		</td></tr>
	
    </table>
<br>
<script language="javascript" src="js/wfloat.js"></script>
<script language="javascript" src="js/right.js"></script>
<?	
}
function save()
{
	/**********************************************************************************************
	获取所有功能点
	**********************************************************************************************/
	//判断当前保存操作是新增记录还是修改记录的条件是：
	//新增记录时，无记录ID
	//编辑内容时有当前记录ID
	Add_Op_Log("修改角色权限");
		//保存修改记录
	$checked = array();
	if(isset($_POST['Id']))
		$checked=$_POST['Id'];
	$user=$_POST['ID'];
	if(count($checked) == 0)
	{
		$sql = "delete from `".RIGHTTABLE."` where Name='$user' and Type='G'";	
		$db->query($sql);
		ANotify::halt("修改用户组权限<font color=red>信息</font>成功！",'SUCCESS');
	}
		else
		{
			$str = implode(",",$checked);
			$sql = "delete from `".RIGHTTABLE."` where Name='$user' and Type='G' and Fun not in($str)";
			$GLOBALS['db']->query($sql);
			$sql = "select Fun from `".RIGHTTABLE."` where Name='$user' and Type='G'";
			$str = array();
			if(($query = $GLOBALS['db']->query($sql))==true)
			{
				while (($row = mysql_fetch_array($query))==true)
					array_push($str,$row[0]);
			}
			$str = array_diff($checked,$str);
			while (list(, $val) = each($str))
			{
				$m	= 0;
				if(!defined("MICRO_RIGHT_SET"))
					$m = 7;
				else 
				{
					if(isset($_POST["fun_$val"]) && ereg("^[0-9]+$",$_POST["fun_$val"]))
						$m	= $_POST["fun_$val"];
				}
				$GLOBALS['db']->query("insert into `".RIGHTTABLE."`(Name,Type,Fun,micro) values('$user','G','".$val."','$m')");
			}
			while (list(, $val) = each($checked))
			{
				$m	= 0;
				if(!defined("MICRO_RIGHT_SET"))
					$m = 7;
				else 
				{
					if(isset($_POST["fun_$val"]) && ereg("^[0-9]+$",$_POST["fun_$val"]))
						$m	= $_POST["fun_$val"];
				}
				$GLOBALS['db']->query("update`".RIGHTTABLE."` set micro='$m' where Fun ='$val' and Name='$user' and Type = 'G'");
			}
			ANotify::halt("修改用户组权限<font color=red>信息</font>成功！",'SUCCESS');
		}	
}
?>